﻿using System;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using Entities = Vision.Core.Entities;
using Components = Vision.Core.Components;

namespace Vision.Web.Pages
{
    public partial class ActivityScheduleEdit : System.Web.UI.Page
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            int activtyId = int.Parse(Request.QueryString["ActivityId"]);
            int scheduleId = int.Parse(Request.QueryString["ScheduleId"]);
            
            if (!Page.IsPostBack)
            {
                using (Components.ActivityCatalog activityCatalog = new Components.ActivityCatalog(ConfigurationManager.AppSettings["VisionCatalog"])){
                    Entities.Activities.VisionActivity activity = activityCatalog.GetVisionActivityFromCatalog(activtyId);
                    this.lblActivtyName.Text = activity.Name;
                    this.lblActivtyDesc.Text = activity.Description;
                    this.lblActivityKey.Text = activity.Key.ToString();
                    this.lblActivtyName.DataBind();
                    this.lblActivtyDesc.DataBind();
                    this.lblActivityKey.DataBind();

                    this.ddlScheduleTypes.DataSource = Enum.GetNames(typeof(Core.Entities.Scheduling.ScheduleTypes));
                    this.ddlScheduleTypes.DataBind();

                    this.cblWeekDays.DataSource = Enum.GetNames(typeof(Core.Entities.Scheduling.ScheduleWeekDays)).Where(s => s != "None");
                    this.cblWeekDays.DataBind();

                    this.cblMonths.DataSource = Enum.GetNames(typeof(Core.Entities.Scheduling.ScheduleMonths)).Where(s => s != "None");
                    this.cblMonths.DataBind();
                }

                Entities.Scheduling.VisionActvitySchedule schedule = new Entities.Scheduling.VisionActvitySchedule();
                schedule.ScheduleType = Entities.Scheduling.ScheduleTypes.Daily;                
                schedule.ScheduleStartDate = DateTime.Now.Date;
                schedule.ScheduleFinishDate = DateTime.Now.Date;
                schedule.RepeatEvery = new TimeSpan(0);

                if (scheduleId > 0){
                    using (Components.ActivityScheduler activityScheduler = new Components.ActivityScheduler(ConfigurationManager.AppSettings["VisionCatalog"])){
                        schedule = activityScheduler.GetVisonScheduleById(scheduleId);
                    }
                }

                //Common:
                this.ddlScheduleTypes.SelectedValue = schedule.ScheduleType.ToString();                
                this.txtDescription.Text = schedule.Description;
                this.chkActive.Checked = schedule.Active;
                this.txtStartDate.Text = schedule.ScheduleStartDate.ToShortDateString();
                this.txtStartTime.Text = schedule.ScheduleStartDate.ToString("HH:m:s");
                this.txtEndDate.Text = schedule.ScheduleFinishDate.ToShortDateString();
                this.txtEndTime.Text = schedule.ScheduleFinishDate.ToString("HH:m:s");
                this.txtRepeatTime.Text = schedule.RepeatEvery.ToString(@"hh\:mm\:ss");

                //Weekly:
                foreach (ListItem item in this.cblWeekDays.Items){
                    if (schedule.RunOnWeekDays.HasFlag((Core.Entities.Scheduling.ScheduleWeekDays)Enum.Parse(typeof(Core.Entities.Scheduling.ScheduleWeekDays), item.Text))){
                        item.Selected = true;
                    }
                }
            }                        
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnBack_Click(object sender, EventArgs e)
        {
            Common.QStringBuilder qsBuilder = new Common.QStringBuilder();
            qsBuilder.Parameters.Clear();
            qsBuilder.Path = "~/Pages/ActivitySchedule.aspx";
            qsBuilder.Parameters.Add("ActivityId", Request.QueryString["ActivityId"]);
            Response.Redirect(qsBuilder.QueryString);                        
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            int activtyId = int.Parse(Request.QueryString["ActivityId"]);
            int scheduleId = int.Parse(Request.QueryString["ScheduleId"]);

            Entities.Scheduling.VisionActvitySchedule schedule = new Entities.Scheduling.VisionActvitySchedule();
            if (scheduleId > 0){
                using (Components.ActivityScheduler activityScheduler = new Components.ActivityScheduler(ConfigurationManager.AppSettings["VisionCatalog"])){
                    schedule = activityScheduler.GetVisonScheduleById(scheduleId);
                }
            }

            schedule.ScheduleType = (Core.Entities.Scheduling.ScheduleTypes)Enum.Parse(typeof(Core.Entities.Scheduling.ScheduleTypes), this.ddlScheduleTypes.SelectedValue);
            schedule.ActivityKey = Guid.Parse(this.lblActivityKey.Text);
            schedule.Active = this.chkActive.Checked;
            schedule.Description = this.txtDescription.Text;
            schedule.ScheduleStartDate = DateTime.Parse(this.txtStartDate.Text + " " + this.txtStartTime.Text);
            schedule.ScheduleFinishDate = DateTime.Parse(this.txtEndDate.Text + " " + this.txtEndTime.Text);
            schedule.ScheduleCanExpire = this.chkExpire.Checked;
            schedule.RepeatEvery = TimeSpan.Parse(this.txtRepeatTime.Text);

            schedule.RunOnWeekDays = Entities.Scheduling.ScheduleWeekDays.None;
            if (this.ddlScheduleTypes.SelectedValue == "Weekly"){
                foreach (ListItem item in this.cblWeekDays.Items){
                    if (item.Selected){
                        schedule.RunOnWeekDays |= (Entities.Scheduling.ScheduleWeekDays)Enum.Parse(typeof(Entities.Scheduling.ScheduleWeekDays), item.Text);
                    }
                }
            }

            using (Components.ActivityScheduler activityScheduler = new Components.ActivityScheduler(ConfigurationManager.AppSettings["VisionCatalog"])){
                activityScheduler.SaveVisonSchedule(schedule);
            }

            Common.QStringBuilder qsBuilder = new Common.QStringBuilder();
            qsBuilder.Parameters.Clear();
            qsBuilder.Path = "~/Pages/ActivitySchedule.aspx";
            qsBuilder.Parameters.Add("ActivityId", Request.QueryString["ActivityId"]);
            Response.Redirect(qsBuilder.QueryString);                        
        }                
    }
}